
<template>
  <el-dialog :title="title" :width="width" :visible.sync="isShowVisible" center :close-on-click-modal="false" :before-close="() => $emit('notRefresh')">
    <el-form label-width="125px" :model="materialData" ref="materialData" :rules="rules" style="min-height:24vw" v-loading="isLoading">
      <el-tabs>
       
          <el-card>
            <el-row>
                <el-col :span="12">
                <el-form-item label="订单编码:" prop="fnumber">
                  <el-input v-model="materialData.fnumber" disabled></el-input>
                </el-form-item>
              </el-col>
                <el-col :span="12">
                <el-form-item label="产品名称:" prop="fworkorderentrtyid" id="fworkorderentrtyid">
                   <inputSelect v-if="!isLoading" :f7Disable="isview" v-model="materialData.fworkorderentrtyid" 
                   :loadShow="materialData.fmaterialname" 
                   :f7Param="materialf7Param" title="分录列表"
                    @onF7DaTaSelected="onF7MaterialSelected" >
                    </inputSelect>
                </el-form-item>
              </el-col>
              
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="产品编码" prop="fmaterialnumber">
                  <el-input v-model="materialData.fmaterialnumber" disabled></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="预计产量" prop="fqty">
                  <el-input v-model="materialData.fqty"  disabled></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="产品型号" prop="fmaterialmodel">
                  <el-input v-model="materialData.fmaterialmodel" disabled></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="产品批号:" prop="fmaterialflot">
                 <el-input v-model="materialData.fmaterialflot" disabled></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              
              <el-col :span="12">
                <el-form-item label="入库数量:" prop="fgoodqty">
                  <el-input v-model="materialData.fgoodqty" :disabled = "isview" type="number"></el-input>
                </el-form-item>
              </el-col>
             <el-col :span="12">
                <el-form-item label="入库时间:" prop="fbizdate">
                  <el-date-picker v-model="materialData.fbizdate" type="datetime" format="yyyy-MM-dd HH:mm:ss" placeholder="选择日期" :disabled="isview">
                      </el-date-picker>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>

                <el-col :span="12">
                <el-form-item label="默认仓库:" prop="fwarehouseid">
                  <mdm-warehouse 
                  v-if="!isLoading" 
                  v-model="materialData.fwarehouseid" 
                  ref="warehouse" 
                  :loadShow="materialData.fwarehousename" 
                  :disabled="isview" 
                  @change="onWarehouseChange"></mdm-warehouse>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="默认库区:" prop="fwareareaid">
                  <mdm-warearea v-if="!isLoading"
                   v-model="materialData.fwareareaid"
                    ref="warearea"
                     :loadShow="materialData.fwareareaname" 
                     :disabled="isview"
                      @change="onWareareaChange"></mdm-warearea>
                </el-form-item>
              </el-col>
              </el-row>
<el-row>
              
              <el-col :span="12">
                <el-form-item label="默认库位:" prop="flocationid">
                  <mdm-waresite v-if="!isLoading"
                   v-model="materialData.flocationid"
                    ref="location" 
                    :loadShow="materialData.flocationname" 
                    :disabled="isview" 
                    @change="onLocationChange"></mdm-waresite>
                </el-form-item>
              </el-col>
                 <el-col :span="12">
                <el-form-item label="操作员:" prop="fuserid" id="fuserid" v-if="isshow">
              <inputSelect v-if="!isLoading" v-model="materialData.fuserid"
               :loadShow="materialData.fusername"
                :f7Param="f7Param"
                :f7Disable="isview" 
                title="员工列表"></inputSelect>            
            </el-form-item>
              </el-col>
            </el-row>
              <el-row>
               
                <el-col :span="12">
                <el-form-item label="产品ID:" prop="fmaterialid" v-if="false">
                 
                </el-form-item>
              </el-col>
              
              </el-row>
          </el-card>
      </el-tabs>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button v-if="!isview" type="primary" @click="saveInStore">保存</el-button>
      <el-button v-if="!isview" @click="$emit('notRefresh')">取消</el-button>
      <el-button v-if="isview" @click="$emit('notRefresh')">关闭</el-button>
    </div>
  </el-dialog>
</template>

<script>
import xuInStoreApi from "@/api/xuSimpleWork/InStoreApi"
import wareHouseApi from '@/api/ware/WareHouseApi'
import wareSiteApi from '@/api/ware/WareSiteApi'
import WareAreaApi from '@/api/ware/WareAreaApi'
export default {
  name: 'instore_add',
  props: {
    width: { type: String, default: '80vw' },
    fid: { type: Number, },
    isview: false,
    operation: { type: String }
  },
  data () {
    return {
        isshow : true,
        fnumberAnable: true,
        title: '创建入库单',
        isShowVisible: true,
        warehouseData: [],
        locationData: [],
        wareareaData: [],
        service  :new xuInStoreApi() ,
        materialf7Param : {
            selectId:'fid',
            selectUrl:'/tie-demo/xuWorkOrderEntry/queryInstoreEntryListPage',
            selectShow:{spt:'',field:['fmaterialname']},
            column:[
           
            {lable:'生产批号',prop:'flot',isFilter:true,filterField:'flot'},
            {lable:'产品',prop:'fmaterialid',isFilter:false,filterField:'fmaterialid'},
            {lable:'产品名称',prop:'fmaterialname',isFilter:false,filterField:'fmaterialname'},
            {lable:'产品简称',prop:'fsimplename',isFilter:false,filterField:'fsimplename'},
            {lable:'预计产量',prop:'fqty',isFilter:false,filterField:'fqty'},
            {lable:'产品类型',prop:'fmaterialmodel',isFilter:true,filterField:'fmaterialmodel'}],
        },
    f7Param : {
        selectId:'userId',
        selectUrl:'/tie-system/entUser/queryListPage',
        selectTreeField:['companyId','deptId'],
        selectTreeUrl:'/tie-system/entUser/queryGroupTree',
        selectShow:{spt:'',field:['userName']},
        column:[{lable:'编号',prop:'userCode',
        isFilter:true,filterField:'userCode'},
        {lable:'名称',prop:'userName',isFilter:true,filterField:'name'}]
      },
    rules:{
        fgoodqty: [{required:true,message:'请填写入库数量'}],
        fuserid: [{required:true,message:'请录入操作员'}],
        fbizdate:[{required:true,message:'请录入入库时间'}],
        fworkorderentrtyid:[{required:true,message:'请录入分录'}],
       fwarehouseid:[{required:true,message:'请录入仓库'}],
    },
      materialData: { },
      isLoading: true,
    }
  },
  
  async created () {
    this.isLoading = true
    await this.$NumRuleUtil.getNumber(this, "cn.hxlc.tie.demo.modular.jf.model.result.XjfInStoreResult", null, 'materialData', 'fnumberAnable', 'fnumber');
    if(this.operation == 'add'){
        }
        else{
          this.isshow = true;
    if(this.operation == 'edit'){
        this.title = "编辑入库单"
          }else if(this.operation == 'view'){
              this.title = "查看入库单";
                this.rules = {};
          }
          this.initSelect();
          let materialData = await this.service.detail({ fid: this.fid })
          this.materialData = materialData.data
          console.log(this.materialData);
      }
      
    this.isLoading = false
  },

  methods: {
    onF7MaterialSelected(row){
      if(row!=null&&row!=''){
this.materialData.fmaterialname = row.fmaterialname;
        this.materialData.fmaterialmodel = row.fmaterialmodel;
        this.materialData.fmaterialnumber = row.fmaterialnumber;
        this.materialData.fqty = row.fqty;
        this.materialData.fmaterialflot = row.flot;
        this.materialData.fmaterialid = row.fmaterialid
      }
      else{
        this.materialData.fmaterialname = ''
        this.materialData.fmaterialmodel = ''
        this.materialData.fmaterialnumber = ''
        this.materialData.fqty = ''
        this.materialData.fmaterialflot = ''
        this.materialData.fmaterialid = ''
      }
        
      },
      async initSelect () {
      let wareHouseHttp = new wareHouseApi();
      let warehouseData = await wareHouseHttp.queryListPage();
      this.warehouseData = warehouseData.data.rows;
      let wareSiteHttp = new wareSiteApi();
      let locationData = await wareSiteHttp.queryListPage();
      this.locationData = locationData.data.rows;
      let WareAreaHttp = new WareAreaApi();
      let wareareaData = await WareAreaHttp.queryListPage();
      this.wareareaData = wareareaData.data.rows;
    },
    saveInStore () {
        let materialData = this.materialData
      this.$refs['materialData'].validate((valid, object) => {
        if (valid) {
          const loading = this.$loading({
            lock: true,
            text: '请耐心等待.....',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.7)'
          })
          //若有fid证明是编辑没有则是新增
          if (this.fid) {
            materialData.fstatus = 1
            this.service.update(this.materialData).then(res => {
              loading.close()
              this.$tie.showSuccess(res.message)
              this.$emit('close')
            }).catch(e => {
              loading.close()
            })
          } else {
            materialData.fstatus = 0
            this.service.add(this.materialData).then(res => {
              loading.close()
              this.$tie.showSuccess(res.message)
              this.$emit('close')
            }).catch(e => {
              loading.close()
            })
          }
        } else {
          let validArr = []
          Object.keys(object).forEach(function (key) {
            validArr.push(key)
          });
          let validVal = "#" + validArr[0];
          document.querySelector(validVal).scrollIntoView(true);
        }
      })
      
    },
    onWarehouseChange (row) {
      //先设置默认值，仓库改变时，设置自身值但不触发值改变事件
      let isClean = this.$tie.isEmpty(row);
      this.materialData.fwarehousename = null
      this.materialData.fwareareaname = null
      this.materialData.flocationname = null
      this.$refs.warehouse.defaultSelect({ fid: null, fname: null, isChange: "0" })
      this.$refs.warearea.defaultSelect({ fid: null, fname: null, isChange: "0" })
      this.$refs.location.defaultSelect({ fid: null, fname: null, isChange: "0" })
      if (!isClean) {
        //有值时设置自身默认值，设置库区过滤条件
        this.materialData.fwarehousename = row.fname
        this.$refs.warehouse.defaultSelect({ fid: row.fid, fname: row.fname, isChange: "0" })
        this.$refs.warearea.refresh([{ field: 'fwarehouseid', value: row.fid }])
        this.$refs.location.refresh([{ field: null, value: null }])
      } else {
        //没值时就是清空，清空设置自身为空，库区为空，库位为空，库区和库位的过滤条件为空
        this.materialData.fwarehousename = null
        this.materialData.fwareareaname = null
        this.materialData.flocationname = null
        this.$refs.warehouse.defaultSelect({ fid: null, fname: null, isChange: "0" })
        this.$refs.warearea.defaultSelect({ fid: null, fname: null, isChange: "0" })
        this.$refs.location.defaultSelect({ fid: null, fname: null, isChange: "0" })

        this.$refs.warearea.refresh([{ field: null, value: null }])
        this.$refs.location.refresh([{ field: null, value: null }])
      }
    },
    onWareareaChange (row) {
    //先设置默认值，库区改变时设置自身值但不触发值改变事件，设置仓库值但不触发值改变事件
      let isClean = this.$tie.isEmpty(row);
      // this.materialData.fwarehousename = null
      this.materialData.fwareareaname = null
      this.materialData.flocationname = null
    // this.$refs.warehouse.defaultSelect({fid:null,fname:null,isChange:"0"})
      this.$refs.warearea.defaultSelect({ fid: null, fname: null, isChange: "0" })
      this.$refs.location.defaultSelect({ fid: null, fname: null, isChange: "0" })
      if (!isClean) {
    //有值时设置仓库、自身默认值，设置库区过滤条件
        this.materialData.fwarehousename = row.fwarehousename
        this.materialData.fwareareaname = row.fname
        this.$refs.warehouse.defaultSelect({ fid: row.fwarehouseid, fname: row.fwarehousename, isChange: "0" })
        this.$refs.warearea.defaultSelect({ fid: row.fid, fname: row.fname, isChange: "0" })
        this.$refs.warehouse.refresh([{ field: null, value: null }])
        this.$refs.warearea.refresh([{ field: 'fwarehouseid', value: row.fwarehouseid }])
        this.$refs.location.refresh([{ field: 'fwareareaid', value: row.fid }])
      } else {
    //没值时就是清空，清空设置自身值为空，库位为空，库位的过滤条件为空
        this.materialData.fwareareaname = null
        this.materialData.flocationname = null
        this.$refs.warearea.defaultSelect({ fid: null, fname: null, isChange: "0" })
        this.$refs.location.defaultSelect({ fid: null, fname: null, isChange: "0" })
        this.$refs.location.refresh([{ field: null, value: null }])
      }
    },
    onLocationChange (row) {
      //先设置默认值，库位改变时设置自身值、库区值、仓库值
      let isClean = this.$tie.isEmpty(row);
      // this.materialData.fwarehousename = null
      // this.materialData.fwareareaname = null
      this.materialData.flocationname = null
      // this.$refs.warehouse.defaultSelect({fid:null,fname:null,isChange:"0"})
      // this.$refs.warearea.defaultSelect({fid:null,fname:null,isChange:"0"})
      this.$refs.location.defaultSelect({ fid: null, fname: null, isChange: "0" })
      if (!isClean) {
        //有值时设置仓库、库区、自身默认值，不触发值更新事件，设置仓库、库区、库位过滤条件
        this.materialData.fwarehousename = row.fwarehousename
        this.materialData.fwareareaname = row.fwareareaname
        this.materialData.flocationname = row.fname
        this.$refs.warehouse.defaultSelect({ fid: row.fwarehouseid, fname: row.fwarehousename, isChange: "0" })
        this.$refs.warearea.defaultSelect({ fid: row.fwareareaid, fname: row.fwareareaname, isChange: "0" })
        this.$refs.location.defaultSelect({ fid: row.fid, fname: row.fname, isChange: "0" })
        this.$refs.warehouse.refresh([{ field: null, value: null }])
        this.$refs.warearea.refresh([{ field: 'fwarehouseid', value: row.fwarehouseid }])
        this.$refs.location.refresh([{ field: 'fwareareaid', value: row.fwareareaid }])
      } else {
        //没值时就是清空，清空自身值为空，库区为空，仓库为空，自身、库区、仓库的过滤条件为空
        // this.materialData.fwarehousename = null
        // this.materialData.fwareareaname = null
        this.materialData.flocationname = null
        // this.$refs.warehouse.defaultSelect({fid:null,fname:null,isChange:"0"})
        // this.$refs.warearea.defaultSelect({fid:null,fname:null,isChange:"0"})
        this.$refs.location.defaultSelect({ fid: null, fname: null, isChange: "0" })
        // this.$refs.warehouse.refresh([{field:null,value:null}])
        // this.$refs.warearea.refresh([{field:null,value:null}])
        // this.$refs.location.refresh([{field:null,value:null}])
      }
    },
  },
}
</script>

<style >
.vxe-select--panel {
  z-index: 3001 !important;
}

 .vxe-input--panel.type--date, .vxe-input--panel.type--month, .vxe-input--panel.type--            week, .vxe-input--panel.type--year {
  z-index: 9998 !important
    }

.col--actived{
  height: 100px !important;

  .vxe-cell{
    height: 100%;

    textarea{
      height: 100%;
    }
  }
}

</style>

